﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using VIFON.BLL;
namespace VIFON.DAL
{
    class DACar
    {
        internal List<Car> GetCarList()
        {
            try
            {
                using (var db = new DBVIFONDataContext())
                {
                    var temp = db.Cars;
                    return temp.Count() > 0 ? temp.ToList() : null;
                }
            }
            catch
            {
                return null;
            }
        }
        internal bool Insert(Car bocar)
        {
            try
            {
                using (var db = new DBVIFONDataContext())
                {
                    db.Cars.InsertOnSubmit(bocar);
                    db.SubmitChanges();
                    return true;
                }
            }
            catch
            {
                return false;
            }
        }
        internal bool Update(Car bocar)
        {
            try
            {
                using (var db = new DBVIFONDataContext())
                {
                    var temp = db.Cars.FirstOrDefault(o => o.CarId == bocar.CarId);
                    if (temp == null)
                        return false;
                    temp.CarId = bocar.CarId;                    
                    db.SubmitChanges();
                    return true;
                }
            }
            catch
            {
                return false;
            }
        }
        internal bool Delete(string CarId)
        {
            try
            {
                using (var db = new DBVIFONDataContext())
                {
                    var temp = db.Cars.FirstOrDefault(o => o.CarId == CarId);
                    if (temp == null)
                        return false;
                    db.Cars.DeleteOnSubmit(temp);
                    db.SubmitChanges();
                    return true;
                }
            }
            catch
            {
                return false;
            }
        }
    }
}
